package org.keco.shape.complex;

import java.awt.Component;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import org.keco.shape.AbstractShape;
import org.keco.shape.Line;

public class ShapeChain extends Component {

	private static final long serialVersionUID = -6225261131810947760L;

	private List<AbstractShape> shapes;

	private AbstractShape tmpShape;

	private AbstractShape selectionShape;

	public ShapeChain() {
		shapes = new ArrayList<AbstractShape>();
	}

	public void addShape(AbstractShape component) {
		shapes.add(component);
	}

	public void removeShape(AbstractShape component) {
		shapes.remove(component);
	}

	@Override
	public void paint(Graphics g) {
		for (AbstractShape shape : shapes) {
			shape.paint(g);
		}
		if (tmpShape != null) {
			tmpShape.paint(g);
		}

		if (selectionShape != null) {
			selectionShape.paint(g);
		}
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < shapes.size(); i++) {
			if (i + 1 == shapes.size()) {
				builder.append(shapes.get(i));
			} else {
				builder.append(shapes.get(i)).append("\n");
			}
		}
		return builder.toString();
	}

	public void fromString(String data) {
		shapes.clear();
		tmpShape = null;
		String[] tokens = data.split("\n");
		for (String token : tokens) {
			if (token.startsWith("L")) {
				Line line = Line.fromString(token);
				if (line != null) {
					shapes.add(line);
				}
			}
		}
	}

	public List<AbstractShape> getShapes() {
		return shapes;
	}

	public void setShapes(List<AbstractShape> shapes) {
		this.shapes = shapes;
	}

	public AbstractShape getTmpShape() {
		return tmpShape;
	}

	public void setTmpShape(AbstractShape tmpShape) {
		this.tmpShape = tmpShape;
	}

	public void deselectAllShapes() {
		for (AbstractShape shape : shapes) {
			shape.deselectShape();
		}
	}

	public AbstractShape getSelectionShape() {
		return selectionShape;
	}

	public void setSelectionShape(AbstractShape selectionShape) {
		this.selectionShape = selectionShape;
	}

}
